# -*- coding: utf-8 -*-

from PyQt5.Qt import *  
import codecs
import os

from libs.ListModule import ListWidget

class Window(QWidget):
    def __init__(self,*args,**kwargs):
        super().__init__()
        self.setWindowTitle("dialogTry")
        self.resize(400, 400)

        self.label_clist = []

        button = QPushButton("对话框",self)
        button.move(20,20)
        button.clicked.connect(self.show_dialog)

        self.load_predefined_classes("D:\\Program Project\\PythonProject\\labeltool\\labeltoolv0.7\\data\\tagsdata.txt")

    def show_dialog(self):
        '''创建对话框'''
        dialog = LabelDialog(self,"输入标签：",self.label_clist)

    def load_predefined_classes(self, tagsdata):
        '''从预分类标签文件中读取标签信息'''
        if os.path.exists(tagsdata) is True:
            with codecs.open(tagsdata, 'r') as f:
                for line in f:
                    line = line.strip()
                    if self.label_clist is None:
                        self.label_clist = [line]
                    else:
                        self.label_clist.append(line)

class LabelDialog(QDialog):
    '''subclass of QDialog'''
    #自定义信号
    add_rec = pyqtSignal(str)
    disadd_rec = pyqtSignal()
    def __init__(self,parent,title,tagsList):
        super().__init__(parent)
        self.setWindowTitle(title)
        self.setModal(True) #设置模态
        self.setWindowFlag(Qt.MSWindowsFixedSizeDialogHint,True)#设置窗口大小固定
        self.layout = QVBoxLayout()

        self.cursor = QCursor()#光标
        self.move(self.cursor.pos())
        #公共变量
        self.tags = tagsList #将传入的与预分类参数保存

        self.edit = QLineEdit()#单行文本输入框

        self.button_box = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel,Qt.Horizontal, self)#添加OK和cancel按钮，设置布局方式为水平

        self.layout.addWidget(self.button_box,alignment=Qt.AlignmentFlag.AlignLeft) #将按钮添加进布局方式同时设置对齐方式为靠左
        self.layout.addWidget(self.edit)

        if self.tags is not None and len(self.tags)>0:
            self.list_widget = ListWidget(self)
            #创造QListWidget控件
            for tag in self.tags:
                self.list_widget.addItem(tag)
            self.list_widget.itemClicked.connect(self.list_click)
            self.list_widget.itemDoubleClicked.connect(self.list_doubleclick)
            self.layout.addWidget(self.list_widget)

        self.setLayout(self.layout)

        self.button_box.accepted.connect(self.save) #点击Ok时链接的动作
        self.button_box.rejected.connect(self.cancel)

        self.show()

    def save(self):
        '''slot'''
        if self.edit.text().strip():
            self.accept()
            self.add_rec.emit(self.edit.text())

    def cancel(self):
        '''slot'''
        self.reject()
        self.disadd_rec.emit()

    def list_click(self,list_item):
        '''slot'''
        text = list_item.text().strip()
        self.edit.setText(text)

    def list_doubleclick(self,list_item):
        '''slot'''
        text = list_item.text().strip()
        self.edit.setText(text)
        self.save()

    def closeEvent(self, QCloseEvent):
        self.disadd_rec.emit()

class LabelDialog2(QDialog):
    '''subclass of QDialog'''
    #自定义信号
    saved = pyqtSignal(str)
    def __init__(self,parent,title,tagsList):
        super().__init__(parent)
        self.setWindowTitle(title)
        self.setModal(True) #设置模态
        self.setWindowFlag(Qt.MSWindowsFixedSizeDialogHint,True)#设置窗口大小固定
        self.layout = QVBoxLayout()

        self.cursor = QCursor()#光标
        self.move(self.cursor.pos())
        #公共变量
        self.tags = tagsList #将传入的与预分类参数保存

        self.edit = QLineEdit()#单行文本输入框

        self.button_box = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel,Qt.Horizontal, self)#添加OK和cancel按钮，设置布局方式为水平

        self.layout.addWidget(self.button_box,alignment=Qt.AlignmentFlag.AlignLeft) #将按钮添加进布局方式同时设置对齐方式为靠左
        self.layout.addWidget(self.edit)

        if self.tags is not None and len(self.tags)>0:
            self.list_widget = ListWidget(self)
            #创造QListWidget控件
            for tag in self.tags:
                self.list_widget.addItem(tag)
            self.list_widget.itemClicked.connect(self.list_click)
            self.list_widget.itemDoubleClicked.connect(self.list_doubleclick)
            self.layout.addWidget(self.list_widget)

        self.setLayout(self.layout)

        self.button_box.accepted.connect(self.save) #点击Ok时链接的动作
        self.button_box.rejected.connect(self.cancel)

        self.show()

    def save(self):
        '''slot'''
        if self.edit.text().strip():
            self.accept()
            self.saved.emit(self.edit.text())

    def cancel(self):
        '''slot'''
        self.reject()

    def list_click(self,list_item):
        '''slot'''
        text = list_item.text().strip()
        self.edit.setText(text)

    def list_doubleclick(self,list_item):
        '''slot'''
        text = list_item.text().strip()
        self.edit.setText(text)
        self.save()

    def closeEvent(self, QCloseEvent):
        self.cancel()

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
